﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill.property
{
    public partial class ModuleUiJavaProperty : XtraUserControl
    {
        private BillUiJavaToProperty bean = null;

        private BillFormatModule bill = null;

        private ProjectRootPanel RootPanel = null;

        private ProjectUnitBillInterface UnitPanel = null;

        public event KEventHandler CaptionChanged = null;

        private string oldType = "";

        public ModuleUiJavaProperty(BillFormatModule _bean, ProjectRootPanel root, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.bill = _bean;
            this.RootPanel = root;
            this.UnitPanel = unit;
            InitProperty();
        }

        private void InitProperty()
        {
            this.Field_One_Caption.Properties.ReadOnly = UnitPanel.ReadOnly;
            this.Field_Two_Key.Properties.ReadOnly = UnitPanel.ReadOnly;
            this.Field_Three_Enable.Properties.ReadOnly = UnitPanel.ReadOnly;
            this.Field_Five_Edit.Properties.ReadOnly = UnitPanel.ReadOnly;
            this.Field_Four_Type.Properties.ReadOnly = UnitPanel.ReadOnly;
            this.RepositoryJavaType.Items.Clear();
            this.RepositoryJavaType.Items.AddRange(BeanUtil.EVENT_PLUGIN_TPEY);
        }

        public BillUiJavaToProperty SelectObject
        {
            set {
                this.bean = value;
                this.PropertyGridJava.SelectedObject = this.bean;
            }
        }

        public BillUiJavaToProperty[] SelectObjects
        {
            set
            {
                this.bean = new BillUiJavaToProperty(value);
                this.PropertyGridJava.SelectedObject = this.bean;
            }
        }

        private void RepositoryJavaCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ProjectUnitBillInterface subject = RootPanel.OpenJavaPanel(UnitPanel, this.bill, this.bean.JavaItem);
            UnitPanel.AddSubjectBill(subject);
        }

        private void PropertyGridJava_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            UnitPanel.SaveStatus = false;
            string javaPackage = string.Format("com.kootop.plugin.{0}", this.bill.getJavaPackage());
            if (e.Row.Properties.FieldName == "Caption")
            {
                if (this.CaptionChanged != null)
                {
                    PropertyDataEvent data = new PropertyDataEvent("", e.Value.ToString());
                    this.CaptionChanged(this, data);
                }
            }else if(e.Row.Properties.FieldName == "Key")
            {
                //this.bean.Key = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(this.bean.Key);
                if (this.bean.JavaItem != null && this.bean.JavaItem.Content == "")
                {
                    if(this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_EVENT)
                    {
                        this.bean.JavaItem.Content = BeanUtil.getJavaCode_default(this.bean.Key, javaPackage);
                    }else if(this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_TIMER)
                    {
                        this.bean.JavaItem.Content = BeanUtil.getJavaCode_Timer(this.bean.Key);
                    }else if(this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_REPORT)
                    {
                        this.bean.JavaItem.Content = BeanUtil.getJavaCode_Report(this.bean.Key, javaPackage);
                    }
                }
            }
            else if(e.Row.Properties.FieldName == "PluginType")
            {
                
                if((this.oldType == BeanUtil.JAVA_PLUGIN_TYPE_EVENT || this.oldType==BeanUtil.JAVA_PLUGIN_TYPE_TIMER||this.oldType==BeanUtil.JAVA_PLUGIN_TYPE_REPORT)&&this.oldType != e.Value.ToString())
                {
                    if (this.bean.JavaItem != null && this.bean.JavaItem.Content != "")
                    {
                        if(XtraMessageBox.Show("扩展代码类型改变，您确认将原代码内容替换为新的内容吗","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
                        {
                            if (this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_EVENT)
                            {
                                this.bean.JavaItem.Content = BeanUtil.getJavaCode_default(this.bean.Key, javaPackage);
                            }
                            else if (this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_TIMER)
                            {
                                this.bean.JavaItem.Content = BeanUtil.getJavaCode_Timer(this.bean.Key);
                            }
                            else if (this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_REPORT)
                            {
                                this.bean.JavaItem.Content = BeanUtil.getJavaCode_Report(this.bean.Key, javaPackage);
                            }
                            else if(this.bean.JavaItem.PluginType == BeanUtil.JAVA_PLUGIN_TYPE_SDK)
                            {
                                this.bean.JavaItem.Content = BeanUtil.getSDKJavaCode(this.bean.Key, javaPackage);
                            }
                        }
                        else
                        {
                            this.bean.PluginType = this.oldType;
                            this.PropertyGridJava.Refresh();
                        }
                    }
                }
            }
        }

        private void PropertyGridJava_CellValueChanging(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            if(e.Row.Properties.FieldName== "PluginType")
            {
                oldType = this.bean.PluginType;
            }
        }
    }
}
